{% extends '::base.html.twig' %}

{% block body %}

<div class="content">
    <div class="form-row row-fluid" id="div_form_bon_livraison">
        {% include "GdsVenteBundle::BonLivraison/form_bon_livraison.html.twig" %}
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function() {
        $('#departement').val({{user.departement.id}})
        $('#departement').attr('disabled', true);
        $('#gds_ventebundle_bonlivraisonclienttype_date').datepicker();
        $('#retour2').hide()
        mdp = ""
        var fullDate = new Date();
        var twoDigitMonth = fullDate.getMonth()
        twoDigitMonth++
        twoDigitMonth += "";
        if (twoDigitMonth.length == 1)
            twoDigitMonth = "0" + twoDigitMonth;
        var twoDigitDate = fullDate.getDate() + "";
        if (twoDigitDate.length == 1)
            twoDigitDate = "0" + twoDigitDate;
        var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
        $("#gds_ventebundle_bonlivraisonclienttype_date").val(currentDate)
        setInterval(time, 1000);
        $('#form_bon_livraison').validate();
        $('#submit_form_bon_livraison').click(function() {
            $('#submit_form_bon_livraison').hide()
            $('#submit_form_bon_livraison_print').hide()
            $('#gds_ventebundle_bonlivraisonclienttype_reference').focus()
            if ($('#form_bon_livraison').valid()) {
                $.ajax({
                    url: '{{path("gds_vente_bon_livraison")}}',
                    type: "POST",
                    datatype: 'json',
                    data: $('#form_bon_livraison').serialize() + '&articles={"articles":' + JSON.stringify(articles) + '}&heure=' + $('#heure').val() + '&facturable=' + $('#facturable').is(':checked') + '&departement=' + $("#departement option:selected").text() + '&mdp=' + mdp,
                    success: function(data) {
                        if (data.success) {
                            $.msgBox({
                                title: "Bon Livraison",
                                content: "Enregistrement Effectué avec succés",
                                type: "info",
                                buttons: [{value: "OK"}],
                                success: function(result) {
                                    location.reload();
                                }
                            });
                        } else if (data.limit_depass) {
                            $.msgBox({
                                title: "Dépassement du Solde Client !!",
                                content: "mdp: <input id='mdp' type='password' />",
                                type: "confirm",
                                buttons: [
                                    {type: "submit", value: "Valider"},
                                    {type: "cancel", value: "Annuler"}
                                ],
                                afterShow: function() {
                                    $('#mdp').focus()
                                },
                                success: function(result) {
                                    if (result == "Valider" && $('#mdp').val() != '') {
                                        mdp = $('#mdp').val()
                                        $('#submit_form_bon_livraison').click()
                                    } else {
                                        $('#submit_form_bon_livraison').show("slow")
                                        $('#submit_form_bon_livraison_print').show("slow")
                                    }
                                }
                            });
                        } else {
                            $.msgBox({
                                title: "Bon Livraison",
                                content: "Erreur D'enregistrement",
                                type: "error",
                                buttons: [{value: "OK"}],
                                success: function(result) {
                                    $('#submit_form_bon_livraison').show("slow")
                                    $('#submit_form_bon_livraison_print').show("slow")
                                }
                            });
                        }
                    },
                    error: function(e) {
                        $.msgBox({
                            title: "Bon Livraison",
                            content: "Erreur D'enregistrement",
                            type: "error",
                            buttons: [{value: "OK"}],
                            success: function(result) {
                                $('#submit_form_bon_livraison').show("slow")
                                $('#submit_form_bon_livraison_print').show("slow")
                            }
                        });
                    }
                })
            }
        })
        $('#submit_form_bon_livraison_print').click(function() {
            $('#submit_form_bon_livraison').hide()
            $('#submit_form_bon_livraison_print').hide()
            $('#gds_ventebundle_bonlivraisonclienttype_reference').focus()
            if ($('#form_bon_livraison').valid()) {
                $.ajax({
                    url: '{{path("gds_vente_bon_livraison")}}',
                    type: "POST",
                    datatype: 'json',
                    data: $('#form_bon_livraison').serialize() + '&articles={"articles":' + JSON.stringify(articles) + '}&heure=' + $('#heure').val() + '&facturable=' + $('#facturable').is(':checked') + '&departement=' + $("#departement option:selected").text() + '&mdp=' + mdp,
                    success: function(data) {
                        if (data.success) {
                            Imprimer(data.id)
                        } else if (data.limit_depass) {
                            $.msgBox({
                                title: "Dépassement du Solde Client !!",
                                content: "mdp: <input id='mdp' type='password' />",
                                type: "confirm",
                                buttons: [
                                    {type: "submit", value: "Valider"},
                                    {type: "cancel", value: "Annuler"}
                                ],
                                afterShow: function() {
                                    $('#mdp').focus()
                                },
                                success: function(result) {
                                    if (result == "Valider" && $('#mdp').val() != '') {
                                        mdp = $('#mdp').val()
                                        $('#submit_form_bon_livraison_print').click()
                                    } else {
                                        $('#submit_form_bon_livraison').show("slow")
                                        $('#submit_form_bon_livraison_print').show("slow")
                                    }
                                }
                            });
                        } else {
                            $.msgBox({
                                title: "Bon Livraison",
                                content: "Erreur D'enregistrement",
                                type: "error",
                                buttons: [{value: "OK"}],
                                success: function(result) {
                                    $('#submit_form_bon_livraison').show("slow")
                                    $('#submit_form_bon_livraison_print').show("slow")
                                }
                            });
                        }
                    },
                    error: function(e) {
                        $.msgBox({
                            title: "Bon Livraison",
                            content: "Erreur D'enregistrement",
                            type: "error",
                            buttons: [{value: "OK"}],
                            success: function(result) {
                                $('#submit_form_bon_livraison').show("slow")
                                $('#submit_form_bon_livraison_print').show("slow")
                            }
                        });
                    }
                })
            }
        })
        Array.prototype.remove = function(v) {
            this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1);
        }
        index = 0;
        nb_article = 1;
        // var index_array = Array();
        articles = new Array();
        // On r�cup�re la balise <div> en question qui contient l'attribut � data-prototype � qui nous int�resse
        $container = $('#gds_ventebundle_lignebonlivraisonclienttype');
    });

</script>

<input type="hidden" id="heure"/>
{% endblock %}    